import Banner from "../../Banner";
import { Constant } from "../Const/Constant";
import AudioManager from "../Managers/AudioManager";
import { UIManager } from "../Managers/UIManager";
import { ZTool } from "../Utils/ZTool";

const { ccclass, property } = cc._decorator;

@ccclass
export default class CommonTipPanel extends cc.Component {
    panel: cc.Node = null;

    /*** 提示标题 */
    titleLb: cc.Label = null;

    /*** 提示内容 */
    contentLb: cc.RichText = null;

    button_1: cc.Node = null;
    button_1_Lb: cc.Label = null;

    button_2: cc.Node = null;
    button_2_Lb: cc.Label = null;

    /*** 按钮1点击回调 */
    cb_1: Function = null;
    /*** 按钮2点击回调 */
    cb_2: Function = null;

    protected onLoad(): void {
        this.panel = cc.find("Panel", this.node);
        this.titleLb = cc.find("Panel/Title/Label", this.node).getComponent(cc.Label);
        this.contentLb = cc.find("Panel/RichText", this.node).getComponent(cc.RichText);
        this.button_1 = cc.find("Panel/Buttons/Button_1", this.node);
        this.button_1_Lb = cc.find("Panel/Buttons/Button_1/Label", this.node).getComponent(cc.Label);
        this.button_2 = cc.find("Panel/Buttons/Button_2", this.node);
        this.button_2_Lb = cc.find("Panel/Buttons/Button_2/Label", this.node).getComponent(cc.Label);
    }

    protected onDisable(): void {
        cc.Tween.stopAllByTarget(this.panel);
        this.panel.setScale(0);
    }

    Show(content: string, button1Str: string = ``, button2Str: string = ``, cb_1: Function = null, cb_2: Function = null, title: string = `提示`) {
        this.onDisable();

        this.contentLb.string = `<b>${content}</b>`;
        this.titleLb.string = title;
        this.button_1_Lb.string = button1Str;
        this.button_2_Lb.string = button2Str;
        this.cb_1 = cb_1;
        this.cb_2 = cb_2;

        this.button_2.active = !ZTool.IsStringNullOrEmpty(button2Str);

        cc.tween(this.panel).to(0.3, { scale: 1 }, { easing: 'backOut' }).call(() => {
            if (Banner.NeedShowMoreAd) {
                Banner.Instance.ShowVideoAd(() => { });

                this.scheduleOnce(() => {
                    Banner.Instance.ShowCustomAd();
                }, 1);
            }
        }).start();
    }

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);

        switch (event.target.name) {
            case "Button_1":
                this.cb_1 && this.cb_1();
                break;
            case "Button_2":
                this.cb_2 && this.cb_2();
                break;
            case "CloseButton":
                UIManager.Instance.HidePanel(Constant.Panel.CommonTipPanel);
                break;
        }
    }
}
